<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Log;

class CrossHttp
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        $response->header('Access-Control-Allow-Origin', '*');

        if ($request->getMethod() === 'OPTIONS') {
            Log::info('OPTIONS',[$request->getScheme()]);
            $response->header('Access-Control-Allow-Methods', '*');
            $response->header('Access-Control-Allow-Credentials', 'true');
            $response->header('Access-Control-Allow-Headers', '*');
        }

        //允许所有来源访问
//        $response->header('Access-Control-Allow-Origin','*');
        //允许访问的方式
//        $response->header('Access-Control-Allow-Method:GET,POST,PUT,PATCH,DELETE,HEAD,OPTIONS,HEAD');
        //允许自定义的头部参数
//        $response->header("Access-Control-Allow-Headers:Origin,X-Requested-With,Content-Type,Accept,Authorization");

       // $response->header('Access-Control-Allow-Headers', 'Origin, Content-Type, Cookie, Accept');
//        $response->header('Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, OPTIONS');
//        $response->header('Access-Control-Allow-Credentials', 'true');  //ession共享的需求才用到
//        var_dump($response);die;
        return $response;
}
    }
